<?php


namespace App\Transformers\Product;


use App\Models\Product;
use App\Transformers\Category\CategoryTransformer;
use App\Transformers\Shipping\ShippingTemplatesTransformer;
use App\Transformers\User\UserLabelsTransformer;
use League\Fractal\TransformerAbstract;

class ProductTransformer extends TransformerAbstract
{

    protected $availableIncludes = [
        'temp',
        'userLabel',
        'productreply',
        'shippingTemplates',
        'productcate',
        'productattrvalue',
        'productattrresult',
        'category',
        'productdescription'
    ];

    protected $defaultIncludes = [
        'productattrresult',
        'productattrvalue',
        'productdescription'
    ];

    public function transform(Product $data)
    {
        return [
            'id' => $data->id ?? '',
            'product_name' => $data->product_name ?? '',
            'keyword' => $data->keyword ?? '',
            'sort' => $data->sort ?? 1,
            'mer_id' => $data->mer_id ?? 0,
            'system_admin_id' => $data->system_admin_id ?? 0,
            'temp_id' => $data->temp_id ?? 0,
            'image' => $data->image ?? '',
            'image_url' => 'https://placeimg.com/640/480/any',
            'recommend_image' => $data->recommend_image ?? '',
            'recommend_image_url' => 'https://placeimg.com/640/480/any',
            'slider_image' => $data->slider_image ?? [],
            'slider_image_url' => ['https://shop--api.oss-cn-beijing.aliyuncs.com/shu1.png','https://shop--api.oss-cn-beijing.aliyuncs.com/shu2.png','https://shop--api.oss-cn-beijing.aliyuncs.com/shu3.png','https://shop--api.oss-cn-beijing.aliyuncs.com/bj.jpg'],
            'product_info' => $data->product_info ?? '',
            'unit_name' => $data->unit_name ?? '',
            'give_integral' => $data->give_integral ?? 0,
            'ficti' => $data->ficti ?? '',
            'code_path' => $data->code_path ?? '',
            'soure_link' => $data->soure_link ?? '',
            'video_link' => $data->video_link ?? '',
            'spu' => $data->spu ?? '',
            'command_word' => $data->command_word ?? '',
            'status' => $data->status ?? '',
            'is_hot' => $data->is_hot ?? '',
            'is_benefit' => $data->is_benefit ?? '',
            'is_best' => $data->is_best ?? '',
            'is_new' => $data->is_new ?? '',
            'is_postage' => $data->is_postage ?? '',
            'mer_use' => $data->mer_use ?? '',
            'is_seckill' => $data->is_seckill ?? 0,
            'is_bargain' => $data->is_bargain ?? 0,
            'is_good' => $data->is_good ?? 0,
            'is_sub' => $data->is_sub ?? 0,
            'is_vip' => $data->is_vip1 ?? 0,
            'spec_type' => $data->spec_type ?? '',
            'activity' => $data->activity ?? '',
            'created_at' => $data->created_at ? $data->created_at->format("Y-m-d H:i:s") : '',
            'updated_at' => $data->updated_at ? $data->updated_at->format("Y-m-d H:i:s") : '',
        ];
    }

    /**
     * 商品详情
     */
    public function includeProductdescription(Product $product)
    {
        $productdescription = $product->productdescription;
        if (empty($productdescription)) {
            return $this->null();
        }
        return $this->item($product->productdescription,new ProductDescriptionTransformer());
    }

    /**
     * 额外的单规格商品详情
     */
    public function includeProductAttrValue(Product $product)
    {
        $productattrvalue = $product->productattrvalue;
        if (empty($productattrvalue)) {
            return $this->null();
        }
        return $this->item($product->productattrvalue,new ProductAttrValueTransformer());
    }

    /**
     * 额外的多规格商品详情
     */
    public function includeProductAttrResult(Product $product)
    {
        return $this->collection($product->productattrresult,new ProductAttrResultTransformer());
    }

    /**
     *额外的运费模板
     */
    public function includeShippingTemplates(Product $product)
    {
        $shippingTemplates = $product->shippingTemplates;
        if (empty($shippingTemplates)) {
            return $this->null();
        }
        return $this->item($product->shippingTemplates,new ShippingTemplatesTransformer());
    }

    /**
     *额外的用户标签
     */
    public function includeUserLabel(Product $product)
    {
        return $this->collection($product->userLabel,new UserLabelsTransformer());
    }

    /**
     *额外的优惠券
     */
    public function includeStoreCouponIssue(Product $product)
    {
        return $this->collection($product->storeCouponIssue,new UserLabelsTransformer());
    }

    /**
     *额外的评论数据
     */
    public function includeProductReply(Product $product)
    {
        return $this->collection($product->productreply,new ProductReplyTransformer());
    }


    /**
     * 额外的分类表数据
     */
    public function includeCategory(Product $product)
    {
        return $this->collection($product->category,new CategoryTransformer());
    }

}
